/**@FILE:WxApiAction.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-5-20 下午1:38:21*/
package com.xf.toy.web;

import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.xf.toy.bean.User;
import com.xf.toy.service.UserService;
import com.xf.toy.service.WxApi;
import com.xf.toy.web.common.BaseAction;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:WxApiAction
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-5-20 下午1:38:21
 *******************************************/
@Controller
public class WxApiAction extends BaseAction {
	@Resource(name = "wxapi")
	public WxApi wxapi;
	@Resource(name = "userService")
	private UserService userService;
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private String  domain="wx.welezu.com";

	@RequestMapping(value = "/toywx")
	public void urlValidate(@RequestParam String signature, String timestamp,
			String nonce, String echostr, HttpServletResponse resp) {
		try {
			wxapi.signUrl(signature, timestamp, nonce, echostr);
			logger.info("signature=" + signature + ",timestamp" + timestamp
					+ ",nonce=" + nonce + ",echostr=" + echostr);
			resp.getWriter().print(echostr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/toy/oauth")
	public void toyOath(@RequestParam(required = false) String redirect_uri,
			@RequestParam(required = false) String code,
			HttpServletRequest req, HttpServletResponse resp) {
		User user = null;
		try {
			if (StringUtils.isBlank(redirect_uri)) {
				redirect_uri = req.getScheme()+"://"+domain+req.getRequestURI();
			}
			logger.info("redirect_uri=" + redirect_uri + ",code=" + code);
			if (StringUtils.isBlank(code)) {
				redirect_uri = wxapi.oAuth2Url(redirect_uri);
				logger.info("wx oath2: redirect_uri=" + redirect_uri);
				resp.sendRedirect(redirect_uri);
			} else {
				//redirect_uri="http://wx.welezu.com/mytoy/test.htm";
				redirect_uri=req.getParameter("toyurl");
				logger.info("toyfront redirect to:"+redirect_uri);
				
				JSONObject object = wxapi.getAccessTokenInfo(code);
				logger.info("get accesstoken info=" + object.toString());
				if (!StringUtils.isBlank(object.getString("openid"))) {
					user = userService.bindUser(object.getString("openid"));
				}
				logger.info("toyfront: redirect_uri=" + redirect_uri);
				resp.sendRedirect(redirect_uri+"?uid="+user.getUserId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			response(resp, e);
		}
	}
	@RequestMapping(value = "/toy/token")
	public void wishcancel(HttpServletRequest req, HttpServletResponse resp) {
		try {
			JSONObject object = wxapi.getToken();
			response(resp, object);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
